package com.dalei.vdserver.webservice;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.dalei.vdserver.dao.DeviceInformationDao;
import com.dalei.vdserver.dao.DeviceStatusDao;
import com.dalei.vdserver.model.DeviceInformation;
import com.dalei.vdserver.model.DeviceStatus;
import com.dalei.vdserver.model.ErrorCode;

public class RegisterDeviceServiceImpl implements RegisterDeviceService {

	public static final String IMEI_REGEX = "^\\d{15}$";

	public static final String IMEI_SV_REGEX = "^\\d{2}$";

	public static final String MAC_REGEX = "^([0-9a-fA-F]{2})(:|-[0-9a-fA-F]{2}){5}$";

	private DeviceInformationDao deviceInformationDao;

	private DeviceStatusDao deviceStatusDao;

	public DeviceInformationDao getDeviceInformationDao() {
		return deviceInformationDao;
	}

	public void setDeviceInformationDao(
			DeviceInformationDao deviceInformationDao) {
		this.deviceInformationDao = deviceInformationDao;
	}

	public DeviceStatusDao getDeviceStatusDao() {
		return deviceStatusDao;
	}

	public void setDeviceStatusDao(DeviceStatusDao deviceStatusDao) {
		this.deviceStatusDao = deviceStatusDao;
	}

	@Override
	public int registerDevice(String model, String imei, String imeiSv,
			String serialNo, String wlanMac, String osVersion) {
		// TODO Auto-generated method stub
		if (imei != null) {
			Pattern pattern = Pattern.compile(IMEI_REGEX);
			Matcher matcher = pattern.matcher(imei);
			if (matcher.find()) {
				if (imeiSv != null) {
					pattern = Pattern.compile(IMEI_SV_REGEX);
					matcher = pattern.matcher(imeiSv);
					if (matcher.find() == false) {
						return ErrorCode.RegisterDevice.INVALID_IMEI_SV;
					}
				}
				if (wlanMac != null) {
					pattern = Pattern.compile(MAC_REGEX);
					matcher = pattern.matcher(wlanMac);
					if (matcher.find()) {
						return ErrorCode.RegisterDevice.INVALID_WLAN_MAC;
					}
				}
				if (isRegistered(imei) == false) {
					DeviceInformation device = new DeviceInformation();
					device.setModel(model);
					device.setImei(imei);
					device.setImeiSv(imeiSv);
					device.setSerialNo(serialNo);
					device.setWlanMac(wlanMac);
					device.setOsVersion(osVersion);
					int deviceId = deviceInformationDao.addDevice(device);
					if (deviceId > 0) {
						DeviceStatus status = new DeviceStatus();
						status.setDeviceId(deviceId);
						status.setCurrentUserId(0);
						Calendar calendar = new GregorianCalendar();
						status.setExpiredTime(calendar.getTime());
						deviceStatusDao.addStatus(status);
						return deviceId;
					} else {
						return ErrorCode.INTERNAL_ERROR;
					}
				} else {
					return ErrorCode.RegisterDevice.ALREADY_REGISTERED;
				}
			} else {
				return ErrorCode.RegisterDevice.INVALID_IMEI;
			}
		} else {
			return ErrorCode.RegisterDevice.NULL_IMEI;
		}
	}

	@Override
	public boolean isRegistered(String imei) {
		// TODO Auto-generated method stub
		List<DeviceInformation> devices = deviceInformationDao.getDevices();
		if (devices != null) {
			for (DeviceInformation device : devices) {
				if (device.getImei().equals(imei)) {
					return true;
				}
			}
			return false;
		} else {
			return false;
		}
	}

	@Override
	public int containsDevice(String imei) {
		// TODO Auto-generated method stub
		List<DeviceInformation> devices = deviceInformationDao.getDevices();
		if (devices != null) {
			for (DeviceInformation device : devices) {
				if (device.getImei().equals(imei)) {
					return device.getId();
				}
			}
			return -1;
		} else {
			return -1;
		}
	}

}